function ALI = gs_GenDTMImage(filenaDTM,OtherNoData,OutName,OptSea) 
%gs_GenDTMImage - generation of an image from DTM
%
%   ALI = gs_GenDTMImage(filenaDTM,OtherNoData,OutName,OptSea)
%
% The input arguments are:
%
%   filenaDTM  : name of input ESRI .asc file or GeoTiff file. If it is 
%                undefined or empty, the filename can be interactively 
%                managed;
%
%   OtherNoData: possible second no-data value (the file already has a
%                no-data value. However, the generation of a DTM by 
%                adding different sources could lead to a second no-data
%                value in elements of the DTM for which there are no 
%                sources). If it is undefined or empty, no a second
%                no-data value is considered;
%
%   OutName    : name of the output files. If it is undefined or empty,
%                the default name 'refDTMIm' is used. If Outname includes
%                the extension, it is used for the image file (allowed
%                extensions: '.jpg','.jpeg','.tif','.tiff'). If Outname
%                does not include the extension, '.tif' is used. In each
%                case, the second generated file has the same name of the
%                image one (except for the extension);
%
%   OptSea     : option about sea. If it is true or is the string 'light'
%                or 'dark', the sea (DTM level zero) is colored in blue.
%                In particular, if OptSea is 'light' or 'dark', the sea is 
%                in light blue or dark blue respectively. If OptSea is 
%                simply true, 'light' is used. If OptSea is undefined or
%                false, the DTM is drawn as a grey-level image using all
%                valid data.
%
% The output variable is the vector ALI = [Xdatamin, Xdatamax, Ydatamin, 
% Ydatamax] taken from the header of the input file (.asc case) or from 
% the metadata (GeoTiff file case).
% See also seaMountMap.

% G. Teza, 2022

if (nargin < 4) || isempty(OptSea)
    OptSea = false;
end
if numel(OptSea) > 1
    OptBlue = OptSea;
    OptSea = true;
end

if (nargin < 3) || isempty(OutName)
    OutName = 'RefDTMIm';
end
if (nargin < 2) || isempty(OtherNoData)
    OtherNoData = [];
end
if (nargin < 1) || isempty(filenaDTM)
    [filen, pathn] = uigetfile(...
        {'*.asc',          'ESRI ArcGIS ASCII files (*.grd)';...
         '*.tif; *.tiff','GeoTiff files (*.tif/.*tiff)'},...
         'INPUT ESRI (.asc) OR GEOTIFF (.tif/.tiff) DTM FILE');
    if filen
        filenaDTM = fullfile(pathn,filen); 
    else
        filenaDTM = [];
    end
end

if isempty(filenaDTM)
    ALI = [];
    warning('Invalid input data');
    return
end

[~,~,extDTM] = fileparts(filenaDTM);
[pathON,nameON,extON] = fileparts(OutName);
if ~isempty(extON)
    OutName = fullfile(pathON,nameON);
    fileIm = [OutName extON];
else
    fileIm  = [OutName '.tif'];
end
fileLim = [OutName '.mat'];

if strcmpi(extDTM,'.asc')
    [A,xmi,xma,~,ymi,yma,~,~,~,~] = gs_dtmread(filenaDTM);
elseif strcmpi(extDTM,'.tif') || strcmpi(ext,'.tiff')
    [A,R] = readgeoraster(filenaDTM);
    A = double(A); 
    xmi = R.XWorldLimits(1);
    xma = R.XWorldLimits(2);
    ymi = R.YWorldLimits(1);
    yma = R.YWorldLimits(2);
end

if ~isempty(OtherNoData)
    nnd = numel(OtherNoData);
    for k = 1:nnd
        Ik = abs(A-OtherNoData(k)) < 1000*eps;
        A(Ik) = NaN;
    end
end

if strcmpi(extDTM,'.asc')
    A = flipud(A);
end

if OptSea
    Am = seaMountMap(A,OptBlue);
else
    Am = uint8(255*(A-min(A(:)))/(max(A(:))-min(A(:))));
    Am = imcomplement(Am);
end

h = figure;
Im = image(Am,'Xdata',[xmi, xma],'Ydata',[ymi yma]);
imwrite(Im.CData,fileIm);
close(h);

ALI = [xmi xma ymi yma];
save(fileLim,'ALI');